﻿using GDGeek;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MrPP.SerriedCabinet
{
    public class SearchProcess : MonoBehaviour
    {
        public BoxModel _model;
        public Task guide(CabinetGroup.ItemData data) {
            return BoxProcess.Instance.open(data);
        }
        public Task search(string word)
        {

            if (string.IsNullOrEmpty(word))
            {
                return new Task();
            }
            string uri = Restful.RestfulManager.Uri("archives");
            var op = Restful.RestfulManager.Instance.options(uri, new System.Collections.Generic.Dictionary<string, string> { { "abbr", word } });
            var task = Restful.RestfulManager.Instance.getArrayTask<ArchivesData>(op, delegate (ArchivesData[] datas)
            {
                model.datas.Clear();
                for (int i = 0; i < datas.Length; ++i) {
                    model.datas.Add(CabinetGroup.Instance.getItem(datas[i]));
                }

            });
            return task;
        }


        [SerializeField]
        public SearchModel model;


        public BoxModel boxModel;
        internal void post(string item1, object item2)
        {
           // Debug.LogError("item1" + item1);
            switch (item1)
            {
                case "box_box":
                    model.isOpen = -1;
                    break;
                case "open_second":
                    if (model.isOpen == (int)item2)
                    {
                        model.isOpen = -1;
                    }
                    else {
                        model.isOpen = (int)item2;
                        _model.box = -1;
                    }
                   
                    break;
                case "screen_guide":
                   // model.selected = -1;
                    doGuide();
                    break;
                case "screen_clear":
                    model.input = "";
                    break;
                case "screen_input":
                    model.input = (string)item2;
                    break;
                case "screen_search":
                    model.selected = -1;
                    doSearch();
                    
                    break;
                case "screen_selected":
                    model.selected = (int)item2;
                    break;
            }
           
        }
        private bool guiding = false;
        private void doGuide()
        {
            if (guiding == false)
            {
                if (model.selected != -1 && model.selected < model.datas.Count) {
                    boxModel.lit = model.datas[model.selected];
                    Task task = this.guide(model.datas[model.selected]);
                    TaskManager.PushFront(task, delegate { guiding = true; });
                    TaskManager.PushBack(task, delegate { guiding = false; });
                    TaskManager.Run(task);
                }
                else
                {
                    boxModel.clearLight();
                }

            }
            
        }

        private bool searching = false;
        private void doSearch()
        {
            if (searching == false)
            {
                Task task = this.search(model.input);
                TaskManager.PushFront(task, delegate { searching = true; });
                TaskManager.PushBack(task, delegate { searching = false; });
                TaskManager.Run(task);
            }
        
            
        }
    }
}